package experiment_2.Complex;

/**
 * Created by Administrator on 2016/4/14.
 */
public class Complex {
    //ComplexNumber的属性
    private double m_dRealPart;//m_dRealPart 实部
    private double m_dImaginaryPart;//m_dImaginaryPart 虚部
    public Complex(double m_dRealPart,double m_dImaginaryPart){
        this.m_dRealPart = m_dRealPart;
        this.m_dImaginaryPart = m_dImaginaryPart;
    }

    //ComplexNumber的方法
    public void Complex(){//ComplexNumber() 构造函数，将实部，虚部都置为0
        this.m_dRealPart = 0;
        this.m_dImaginaryPart = 0;
    }
    public void Complex(double r,double i){//ComplexNumber(double r,double i) 构造函数，创建复数对象的同时完成复数的实部、虚部的初始化
        this.m_dRealPart = r;
        this.m_dImaginaryPart = i;
    }
    public double GetRealPart(){//GetRealPart() 获取实部
        return this.m_dRealPart;
    }
    public double GetImaginaryPart(){//GetImaginaryPart() 获取虚部
        return this.m_dImaginaryPart;
    }
    public void SetRealPart(double d){//SetRealPart(double d) 设置实部
        this.m_dRealPart = d;
    }
    public void SetImaginaryPart(double d){//SetImaginaryPart(double d) 设置虚部
        this.m_dImaginaryPart = d;
    }
    public Complex Add(Complex c){//ComplexAdd(ComplexNumber c) 复数相加
        return new Complex(m_dRealPart+c.m_dRealPart,m_dImaginaryPart+c.m_dImaginaryPart);
    }
    //ComplexAdd(double c) 复数相加
    public Complex Minus(Complex c){//ComplexMinus(ComplexNumber c) 复数相减
        return new Complex(m_dRealPart-c.m_dRealPart,m_dImaginaryPart-c.m_dImaginaryPart);
    }
    //ComplexMinus(double c) 复数相减
    public Complex Multi(Complex c){//ComplexMulti(ComplexNumber c) 复数相乘
        return new Complex(m_dRealPart*c.m_dRealPart-m_dImaginaryPart*c.m_dImaginaryPart,
                m_dRealPart*c.m_dImaginaryPart+m_dImaginaryPart*c.m_dRealPart);
    }
    //ComplexMulti(double c) 复数相乘
    public String toString(){//toString()把当前复数对象的实部，虚部组合成a+bi的字符串形式
        String s;
        if (m_dRealPart != 0.0){
            if (m_dImaginaryPart > 0)
                s = new Float(m_dRealPart).toString() + "+" + new Float(m_dImaginaryPart).toString() + "i";
            else if (m_dImaginaryPart < 0)
                s = new Float(m_dRealPart).toString() + "-" + new Float(-1*m_dImaginaryPart).toString() + "i";
            else
                s = new Float(m_dRealPart).toString();
        }
        else {
            if (m_dImaginaryPart > 0)
                s = new Float(m_dImaginaryPart).toString() + "i";
            else if (m_dImaginaryPart< 0)
                s = new Float(-1*m_dImaginaryPart).toString() + "i";
            else
                s = new Float(m_dRealPart).toString();
        }
        return s;
    }
    public void putout(Complex c){
        System.out.printf("%s%n",toString());
    }
}